package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.mapper.SportMapper;
import com.srzk.youthleagueback.mapper.SportPicMapper;
import com.srzk.youthleagueback.mapper.ThematicEducationMapper;
import com.srzk.youthleagueback.mapper.ThematicPicMapper;
import com.srzk.youthleagueback.pojo.*;
import com.srzk.youthleagueback.results.Result;
import com.srzk.youthleagueback.utils.FileUtil;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.srzk.youthleagueback.results.CodeMsg.*;
import static com.srzk.youthleagueback.results.CodeMsg.NOT_EXIST;

@Service
public class SportService {
    @Autowired
    private SportMapper sportMapper;
    @Autowired
    private SportPicMapper sportPicMapper;

    public Result<Map<String,Long>> addSport(Sport sport, MultipartFile[] files) {
        int result = sportMapper.insert(sport);
        if (result == 1) {
            List<String> url = new ArrayList<>();
           if(files!=null){
               try {
                   for(MultipartFile file:files){
                       //存储处理
                       String path =  FileUtil.handlePicture("part4",file);
                       SportPic sportPic = new SportPic();
                       sportPic.setPicture(path);
                       sportPic.setId(sportPic.getId());
                       sportPicMapper.insert(sportPic);
                   }
               }catch (Exception e){
                   return Result.error(File_FAIL);
               }
           }
            HashMap<String,Long> map = new HashMap<>();
            map.put("id",sport.getId());
            return Result.success(map);
        } else {
            return Result.error(ADD_FAIL);
        }
    }

    public Result<Map<String,Object>> getGradeList(String grade, String year) {
        List<Sport> allData = sportMapper.selectByGrade(year);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();

            allData.forEach(items -> {
                List<Map<String, Object>> list = new ArrayList<>();
                if (ObjectUtil.getGrade(items.getUser()).equals(grade)) {
                    String getClass = ObjectUtil.getClass(items.getUser());

                    Map<String,Object> listItem = new HashMap<>();
                    listItem.put("date",items.getDate());
                    listItem.put("form",items.getForm());
                    listItem.put("address",items.getAddress());
                    listItem.put("theme",items.getTheme());

                    boolean flag = false;
                    for (Map<String, Object> stringObjectMap : data) {
                        if (getClass.equals(stringObjectMap.get("class"))) {
                            ArrayList tempList = (ArrayList) stringObjectMap.get("list");
                            tempList.add(listItem);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        Map<String, Object> dataItem = new HashMap<>();
                        dataItem.put("class", getClass);
                        list.add(listItem);
                        dataItem.put("list", list);
                        data.add(dataItem);
                    }
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> getHistoryList(String user) {
        List<Sport> allData = sportMapper.selectAll(user);
        if (allData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();
            allData.forEach(items -> {
                ArrayList<Map<String, Object>> list;

                Map<String,Object> listItem = new HashMap<>();
                listItem.put("date",items.getDate());
                listItem.put("form",items.getForm());
                listItem.put("address",items.getAddress());
                listItem.put("theme",items.getTheme());

                boolean flag = false;
                for (Map<String, Object> datum : data) {
                    if(items.getYear().equals(datum.get("year"))){
                        list = (ArrayList<Map<String, Object>>) datum.get("list");
                        list.add(listItem);
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    list = new ArrayList<>();
                    Map<String, Object> dataItem = new HashMap<>();
                    dataItem.put("year", items.getYear());
                    list.add(listItem);
                    dataItem.put("list", list);
                    data.add(dataItem);
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public  Result<String> deleteSport(String user, String year, Long id) {
        sportPicMapper.deleteByUserAndYearAndTypeAndId(id);
        int result = sportMapper.deleteByUserAndYearAndTypeAndId(user, year, id);
        if(result==1){

            return Result.success("删除成功");
        }else{
            return Result.error(DELETE_FAIL);
        }
    }

    public  Result<String> updateSport(Sport sport,MultipartFile[] files) {
        sportPicMapper.deleteByUserAndYearAndTypeAndId(sport.getId());
        int result = sportMapper.updateByPrimaryKey(sport);
        if(result==1){
            List<String> url = new ArrayList<>();
            if(files!=null){
                try {
                    for(MultipartFile file:files){
                        String path =  FileUtil.handlePicture("part4",file);
                        SportPic sportPic = new SportPic();
                        sportPic.setPicture(path);
                        sportPic.setId(sport.getId());
                        sportPicMapper.insert(sportPic);
                    }
                }catch (Exception e){
                    return Result.error(File_FAIL);
                }
            }
            return Result.success("更新成功");
        }else{
            return Result.error(UPDATE_FAIL);
        }
    }

    public Result<Map<String,Object>> getSportList(String user, String year) {
        List<Sport> sports = sportMapper.selectByPrimaryKeyList(user, year);
        List<Map<String,Object>> educationList = new ArrayList<>();
        Map<String,Object> result = new HashMap<>();
        if(sports!=null){
            sports.forEach(item ->{
                List<String> url = new ArrayList<>();
                List<SportPic> pics = sportPicMapper.selectByPrimaryKeyLiist(item.getId());
                for(SportPic pic:pics){
                    url.add(pic.getPicture());
                }
                //urls.add(url);
                Map<String,Object> objectMap = new HashMap<>();
                Map<String,Object> info = new HashMap<String, Object>();
                info.put("date",item.getDate());
                info.put("address",item.getAddress());
                info.put("form",item.getForm());
                info.put("theme",item.getForm());
                info.put("count",item.getCount());
                info.put("content",item.getContent());
                objectMap.put("info",info);
                objectMap.put("id",item.getId());
                objectMap.put("pictures",url);
                educationList.add(objectMap);
            });
            result.put("sportList",educationList);

            return Result.success(result);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String,Object>> getSport(String user, String year,Long id) {
        Sport sport = sportMapper.selectByPrimaryKey(id,user, year);
        if(sport!=null){
            List<String> url = new ArrayList<>();
            List<SportPic> pics = sportPicMapper.selectByPrimaryKeyLiist(sport.getId());
            for(SportPic pic:pics){
                url.add(pic.getPicture());
            }
            Map<String,Object> objectMap = new HashMap<>();
            Map<String,Object> info = new HashMap<String, Object>();
            info.put("date",sport.getDate());
            info.put("address",sport.getAddress());
            info.put("form",sport.getForm());
            info.put("theme",sport.getForm());
            info.put("count",sport.getCount());
            info.put("content",sport.getContent());
            objectMap.put("id",sport.getId());
            objectMap.put("pictures",url);

            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

}
